Partial Class pages_Speakers_Speaker : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property SpeakerId() As Byte
        Get
            Return CWeb.RequestByte("speakerId", 0)
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return SpeakerId > 0
        End Get
    End Property
#End Region

#Region "Members"
    Private m_speaker As CSpeaker
#End Region

#Region "Data"
    Public ReadOnly Property [Speaker]() As CSpeaker
        Get
            If IsNothing(m_speaker) Then
                If IsEdit Then
                    m_speaker = CSpeaker.Cache.GetById(SpeakerId)
                    If IsNothing(m_speaker) Then CSitemap.RecordNotFound("Speaker", SpeakerId)
                Else
                    m_speaker = New CSpeaker
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_speaker
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.SpeakerEdit(Me.Speaker.SpeakerId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Speakers)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        imgSpeakerImage.FolderPath = CSitemap.SpeakerUploads

        ddSpeakers.DataSource = CSpeaker.Cache
        ddSpeakers.DataBind()
        CDropdown.BlankItem(ddSpeakers, "-- New Speaker --", 0)
        CDropdown.SetValue(ddSpeakers, SpeakerId)

        'Page Title
        If IsEdit Then            
            Me.Title = "Speaker Details"
            lnkProducts.Text = CUtilities.CountSummary(Speaker.Products, "sermon", "none")
            lnkProducts.NavigateUrl = CSitemap.ProductsForSpeaker(SpeakerId)
        Else
            Me.Title = "Create New Speaker"
            lnkProducts.Visible = False
        End If
                
        'Textbox logic
        'Me.Form.DefaultFocus = txtSpeakerName.Textbox.ClientID;
        Me.Form.DefaultButton = btnSave.UniqueID   'txtSpeakerName.OnReturnPress(btnSave)

        'Button Text
        btnDelete.Visible = IsEdit
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Speaker"
        If IsEdit Then AddButton(CSitemap.SpeakerAdd(), "Create a new Speaker")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadSpeaker()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveSpeaker()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.Speaker.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
    Protected Sub imgSpeakerImage_DeleteClick() Handles imgSpeakerImage.DeleteClick
        With Me.Speaker
            .SpeakerImage = String.Empty
            .Save()
        End With
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadSpeaker()
        With Me.Speaker
            txtSpeakerName.Text = .SpeakerName
            imgSpeakerImage.FileName = .SpeakerImage
            txtSpeakerAbout.Text = .SpeakerAbout
            txtSpeakerContactPhone.Text = .SpeakerContactPhone
            txtSpeakerContactEmail.Text = .SpeakerContactEmail
            txtSpeakerWebsite.Text = .SpeakerWebsite
        End With
    End Sub
    Protected Sub SaveSpeaker()
        With Me.Speaker
            .SpeakerName = txtSpeakerName.Text
            If imgSpeakerImage.HasFile Then
                imgSpeakerImage.FolderPath = CSitemap.SpeakerUploads(False)
                .SpeakerImage = imgSpeakerImage.SaveFile
            End If
            .SpeakerAbout = txtSpeakerAbout.Text
            .SpeakerContactPhone = txtSpeakerContactPhone.Text
            .SpeakerContactEmail = txtSpeakerContactEmail.Text
            .SpeakerWebsite = txtSpeakerWebsite.Text.Trim()

            If .SpeakerWebsite.StartsWith("http://") Then .SpeakerWebsite = .SpeakerWebsite.Substring(7)

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region


    Protected Sub ddSpeakers_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSpeakers.SelectedIndexChanged
        Response.Redirect(CSitemap.SpeakerEdit(CDropdown.GetByte(ddSpeakers)))
    End Sub
End Class
